<?php
namespace Admin\Controller;
use Think\Controller;
/**
* UploadController
*/
class UploadController extends Controller
{
	public function fields()
    {
        $id = I('get.id');
        $type = I('get.type');
        if ($type == 'image') {
            $num = 1;
        } else {
            $num = M('configs')->where("name='CFG_IMGTHUMB_NUM'")->getField('value');
        }
        $this->assign('num',$num);
        $this->assign('fieldname',$id);
    	$this->display();
    }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg', 'bmp');// 设置附件上传类型
        $upload->rootPath  =     './Upload/'; // 设置附件上传根目录
        $upload->savePath  =     '/image/'; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            echo $upload->getError();
        }else{// 上传成功
        	foreach($info as $file){
                $path=__ROOT__.'/Upload'.$file['savepath'].$file['savename'];
                $arr=array();
                $arr['path'] = $path;
                $arr['type'] = 1;
                $id = M('Files')->add($arr);
                $arr['id'] = $id;
                echo json_encode($arr);
            }
        }
    }
    public function del($src)
    {
    	$path = ".".$src;
        $res = M('Files')->where("path='".$src."'")->delete();
        if ($res) {
            echo "1";
        } else {
            echo "0";
        }
    	if (file_exists($path)) {
    		unlink($path);
    	} else {
    		echo "0";
    	}
    }
}